<?php

namespace app\admin\model\information;
use think\Model;
use app\admin\model\Kisapi;
use app\admin\model\information\Unit;

/*
 * 金蝶档案
 * 取api接口
 * 做本地缓存，无缓存则生成
 * 做成计划任务，没3分钟更新1次
 *   */

class KisBaseinfo extends Model
{
    /*
     * 获取供应商
     * $rtAllData  是否返回全部数组键,得重新获取数组$tonew = true
     * $tonew 是否重新获取
     *   */
    public static function getSupplier($tonew = false,$rtAllData = false,$condition=[],$pageflag = false){
        $cache = cache('kis_supplier');
        
        //空，或者true 重新生成缓存
        if(!$cache||$tonew){
            $rows = Kisapi::getDataByName('supplier',$condition,$pageflag);
            if($pageflag){
                $rows = $rows['data_item'];
            }
            if($rows){
                if($rtAllData){
                    $cache = $rows;
                }else{
                    $id = array_column($rows, 'fItemId');
                    $name = array_column($rows, 'fName');
                    $cache = array_combine($id, $name);
                    cache('kis_supplier',$cache);
                }
            }
        }
        
        return $cache;
    }
    
    /*
     * 获取部门
     * $rtAllData  是否返回全部数组键,得重新获取数组$tonew = true
     * $tonew 是否重新获取
     *   */
    public static function getDepartment($tonew = false,$rtAllData = false){
        $cache = cache('kis_department');
        
        //空，或者true 重新生成缓存
        if(!$cache||$tonew){
            $rows = Kisapi::getDataByName('department');

            if($rows){
                if($rtAllData){
                    $cache = $rows;
                }else{
                    $id = array_column($rows, 'fItemId');
                    $name = array_column($rows, 'fName');
                    $cache = array_combine($id, $name);
                    cache('kis_department',$cache);
                }
            }
        }
        
        return $cache;
    }
    
    /*
     * 获取员工
     * $rtAllData  是否返回全部数组键,得重新获取数组$tonew = true
     * $tonew 是否重新获取
     *   */
    public static function getEmployee($tonew = false,$rtAllData = false){
        $cache = cache('kis_employee');
        
        //空，或者true 重新生成缓存
        if(!$cache||$tonew){
            $rows = Kisapi::getDataByName('employee');
            if($rows){
                if($rtAllData){
                    $cache = $rows;
                }else{
                    $id = array_column($rows, 'fItemId');
                    $name = array_column($rows, 'fName');
                    $cache = array_combine($id, $name);
                    cache('kis_employee',$cache);
                }
            }
        }
        
        return $cache;
    }
    
    /*
     * 获取当前用户
     * $rtAllData  是否返回全部数组键,得重新获取数组$tonew = true
     * $tonew 是否重新获取
     *   */
    public static function getUser($tonew = false,$rtAllData = false){
        $cache = cache('kis_user');
        
        //空，或者true 重新生成缓存
        if(!$cache||$tonew){
            $rows = Kisapi::getDataByName('user');//,['FUserTypeID'=>1001]
            if($rows){
                if($rtAllData){
                    $cache = $rows;
                }else{
                    $id = array_column($rows, 'fUserID');
                    $name = array_column($rows, 'fName');
                    $cache = array_combine($id, $name);
                    cache('kis_user',$cache);
                }
            }
        }
        
        return $cache;
    }
    
    /*
     * 获取客户
     * $rtAllData  是否返回全部数组键,得重新获取数组$tonew = true
     * $tonew 是否重新获取
     * $customer = KisBaseinfo::getCustomer(true,true,[
            "fAcctNum" => "001",
            "fItemId" => 42543,
            "PageSize" => 1,
            "PageIndex" => 1
        ],true);
     *   */
    public static function getCustomer($tonew = false,$rtAllData = false,$condition=[],$pageflag = false){
        $cache = cache('kis_customer');
        
        //空，或者true 重新生成缓存
        if(!$cache||$tonew){
            $rows = Kisapi::getDataByName('customer',$condition,$pageflag);
            if(isset($condition['fItemId'])){
                return $rows['data_item'][0];
            }
            $rows = $rows['data_item'];
            if($rows){
                if($rtAllData){
                    $cache = $rows;
                }else{
                    $id = array_column($rows, 'fItemId');
                    $name = array_column($rows, 'fName');
                    
                    $cache = array_combine($id, $name);
                    cache('kis_customer',$cache);
                }
            }
        }
        
        return $cache;
    }
    
    /*
     * 获取单位
     * $rtAllData  是否返回全部数组键,得重新获取数组$tonew = true
     * $tonew 是否重新获取
     *   */
    public static function getUnit($tonew = false,$rtAllData = false){
        $cache = cache('kis_unit');
        
        //空，或者true 重新生成缓存
        if(!$cache||$tonew){
            $rows = Kisapi::getDataByName('unit');
            if($rows){
                if($rtAllData){
                    $cache = $rows;
                }else{
                    $id = array_column($rows, 'fItemId');
                    $name = array_column($rows, 'fName');
                    $cache = array_combine($id, $name);
                    cache('kis_unit',$cache);
                }
                
                db('unit')->execute('TRUNCATE TABLE fa_unit');

                //更新本地unit表
                db('unit')->insertAll($rows,true);

                //增加换算率缓存
                $kis_unit_coefficient = db('unit')->column('fCoefficient','fItemId');
                cache('kis_unit_coefficient',$kis_unit_coefficient);
            }
        }
        
        return $cache;
    }
    
    /*
     * 获取物料分类
     * $rtAllData  是否返回全部数组键,得重新获取数组$tonew = true
     * $tonew 是否重新获取
     *   */
    public static function getIcItemClass($tonew = false,$rtAllData = false){
        $cache = cache('kis_itemclass');
        
        //空，或者true 重新生成缓存
        if(!$cache||$tonew){
            $rows = Kisapi::getDataByName('inventoryclass');
            if($rows){
                if($rtAllData){
                    $cache = $rows;
                }else{
                    $id = array_column($rows, 'fItemId');
                    $name = array_column($rows, 'fName');
                    $cache = array_combine($id, $name);
                    cache('kis_itemclass',$cache);
                }
            }
        }
        
        return $cache;
    }
    
    /*
     * 获取仓库
     * $rtAllData  是否返回全部数组键,得重新获取数组$tonew = true
     * $tonew 是否重新获取
     * [
     *  1=>'仓库名'
     * ]
     *   */
    public static function getStock($tonew = false,$rtAllData = false){
        $cache = cache('kis_itemclass');
        
        //空，或者true 重新生成缓存
        if(!$cache||$tonew){
            $rows = Kisapi::getDataByName('warehouse');
            if($rows){
                if($rtAllData){
                    $cache = $rows;
                }else{
                    $id = array_column($rows, 'fItemId');
                    $name = array_column($rows, 'fName');
                    $cache = array_combine($id, $name);
                    $kis_warehouse_arr = [];
                    foreach($cache as $k=>$v){
                        $kis_warehouse_arr[$k] = $v;
                    }
                    cache('kis_itemclass',$kis_warehouse_arr);
                }
            }
        }
        return $cache;
    }
    
}
